import Vue from 'vue'
import Vuex from 'vuex'
import * as auth from '../utils/auth'
import { login } from '@/api/user'

import chat from './modules/chat'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: auth.getToken() || {}
  },
  mutations: {
    setUser (state, token) {
      state.user = token
      auth.setToken(token)
    },
    delUser (state) {
      state.user = {}
      auth.removeToken()
    }
  },
  actions: {
    async setToken ({ commit }, form) {
      const { data } = await login(form)
      console.log(data)
      commit('setUser', data)
    }
  },
  modules: {
    chat
  }
})
